package com.android.pagemarket.ui.activity.schedul;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.android.pagemarket.R;
import com.android.pagemarket.io.protocol.HttpManager;
import com.android.pagemarket.io.protocol.in.HttpAction.ActionListener;
import com.android.pagemarket.io.protocol.in.SchedulAction;
import com.android.pagemarket.io.protocol.out.SchedulList;
import com.android.pagemarket.ui.activity.base.TemplateActivity;
import com.android.pagemarket.ui.widget.SchedulView;
import com.android.pagemarket.ui.widget.TitleBar;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-11-2
 * @see : 生产排期视图
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class SchedulActivity extends TemplateActivity implements OnClickListener
{
    private static SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat formater2 = new SimpleDateFormat("yyyy年MM月dd日");
    private View today_lay;
    private View tomorrow_lay;
    private TextView tx_today;
    private TextView tx_today_date;
    private TextView tx_tomorrow;
    private TextView tx_tomorrow_date;
    private SchedulView schedulView;
    private boolean isToday = true;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_schedul);
        initViews();
    }
    
    @Override
    protected void setupTitleBar(TitleBar titleBar)
    {
        super.setupTitleBar(titleBar);
        titleBar.setTitle(R.string.title_shedule);
    }
    
    private void initViews()
    {
        today_lay = getViewById(R.id.today_lay);
        tomorrow_lay = getViewById(R.id.tomorrow_lay);
        schedulView = getViewById(R.id.schedulView);
        tx_today = getViewById(R.id.tx_today);
        tx_today_date = getViewById(R.id.tx_today_date);
        tx_tomorrow = getViewById(R.id.tx_tomorrow);
        tx_tomorrow_date = getViewById(R.id.tx_tomorrow_date);
        
        Calendar cal = Calendar.getInstance();
        String timestamp = formater2.format(cal.getTime()); 
        tx_today_date.setText(timestamp);
        cal.add(Calendar.DAY_OF_MONTH, 1);
        timestamp = formater2.format(cal.getTime());
        tx_tomorrow_date.setText(timestamp);
        
        updateSelectedViews();
    }
    
    @Override
    public void refreshView()
    {
        load(isToday);
    }
    
    private void updateSelectedViews()
    {
        today_lay.setSelected(isToday);
        tx_today.setSelected(isToday);
        tx_today_date.setSelected(isToday);       
        tomorrow_lay.setSelected(!isToday);
        tx_tomorrow.setSelected(!isToday);
        tx_tomorrow_date.setSelected(!isToday);
        
        load(isToday);
    }
    
    public static void startActivity(Context context)
    {
        Intent it = new Intent(context, SchedulActivity.class);       
        context.startActivity(it);
    }

    @Override
    public void onClick(View v)
    {
        switch(v.getId())
        {
            case R.id.today_lay:
            {
                if(!isToday)
                {
                    isToday = true;
                    updateSelectedViews();
                }
                break;
            }
            case R.id.tomorrow_lay:
            {
                if(isToday)
                {
                    isToday = false;
                    updateSelectedViews();
                }
                break;
            }
        }
    } 
    
    private void load(boolean isToday)
    {   
//        showLoading(true);
        showLoding(null, "载入中...");
        Calendar cal = Calendar.getInstance();
        if(!isToday)
        {
            cal.add(Calendar.DAY_OF_MONTH, 1);
        }
        String timestamp = formater.format(cal.getTime());       
        SchedulAction action = new SchedulAction(timestamp);
        action.setActionListener(new ActionListener<SchedulList>()
        {
            @Override
            public void onSucceed(SchedulList result)
            {
//                showLoading(false);
                hideLoding();
                if(result.isNoError())
                {                   
                    schedulView.bindData(result.lines);
                }else
                {
                    schedulView.bindData(null);
                    showToast(result.resultInfo);
                }
            }
            @Override
            public void onError(int resultCode)
            {
//                showLoading(false);
                hideLoding();
                schedulView.bindData(null);
                showToast(SchedulAction.getErrorMean(resultCode));
            }
        });
        HttpManager.getInstance().submitAction(action);
    }
}
